<template>
  <div>
    <el-dialog :visible="visible"
               title="基地地块"
               @close="visible = false">
      <div class="operations">
        <el-button type="primary"
                   @click="openForm()">新增</el-button>
      </div>
      <el-table :data="dataList"
                stripe
                style="width: 100%">
        <el-table-column prop="areaName"
                         label="地块名称" />
        <el-table-column prop="areaNumber"
                         :show-overflow-tooltip="true"
                         label="地块号" />
        <el-table-column prop="remark"
                         :show-overflow-tooltip="true"
                         label="备注" />
        <el-table-column prop="plantId"
                         label="种植批次" />
        <el-table-column prop="remark"
                         label="备注" />
        <el-table-column label="操作"
                         width="300">
          <template #default="scope">
            <el-button type="success"
                       v-if="!scope.row.plantId"
                       @click="plantHandle(scope.row)">种植</el-button>
            <el-button type="warning"
                       v-if="scope.row.plantId"
                       @click="harvestHandle(scope.row)">采摘</el-button>
            <el-button type="primary"
                       @click="openForm(scope.row)">编辑</el-button>
            <el-button type="danger"
                       @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <Form ref="form"
          @refresh="load" />
    <PlantForm ref="plantForm"
               @refresh="load" />
    <HarvestForm ref="harvestForm"
                 @refresh="load" />
  </div>
</template>

<script>
import { getAreaByCompanyId, deleteArea } from '@/api/plant/area'
import Form from './areaForm.vue'
import PlantForm from "./plantForm.vue"
import HarvestForm from "./harvestForm.vue"
export default {
  components: { Form, PlantForm, HarvestForm },
  data () {
    return {
      dataList: [],
      searchForm: {
        companyName: '',
        remark: ''
      },
      companyInfo: {},
      visible: false
    }
  },
  methods: {
    init (company) {
      console.log(company)
      this.companyInfo = company
      this.visible = true;
      this.load()
    },
    async load () {
      const data = await getAreaByCompanyId(this.companyInfo.companyId)
      this.dataList = data.data
    },
    pageChange (page) {
      this.pageNum = page
      this.load()
    },
    pageSizeChange (size) {
      this.pageSize = size
      this.load()
    },
    handleSelectionChange (rows) {
      this.seleteList = rows
    },
    openForm (data) {
      this.$refs.form.init(data, this.companyInfo.companyId)
    },
    plantHandle (data) {
      this.$refs.plantForm.init(data)
    },
    harvestHandle (data) {
      this.$refs.harvestForm.init(data)
    },
    deleteHandle (id) {
      this.$confirm(
        '确定要删除吗？',
        'Warning',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        let ids = []
        if (id) {
          ids = [id]
        } else {
          this.seleteList.forEach(item => {
            ids.push(item.id)
          })
        }
        deleteArea([id]).then(() => {
          this.load()
          this.$message({
            type: 'success',
            message: '操作成功'
          })
        }).catch(() => {
          this.$message({
            type: 'danger',
            message: '操作失败'
          })
        })
      }).catch(() => { })
    },
  }
}
</script>

<style lang="scss" scoped>
.search-form {
  margin: 10px 0;
  padding: 0 10px;
  display: flex;
  .el-form-item {
    margin-right: 20px;
  }
}
.arrow {
  cursor: pointer;
}

.arrow:hover {
  color: #409eff;
}
.operations {
  padding: 0 10px;
}
</style>
